package com.oddfar.campus.business.controller;

import com.oddfar.campus.business.domain.Wzxx;
import com.oddfar.campus.business.mapper.WzxxMapper;
import com.oddfar.campus.business.service.WzxxService;
import com.oddfar.campus.common.domain.PageResult;
import com.oddfar.campus.common.domain.R;
import com.oddfar.campus.common.domain.model.LoginUser;
import com.oddfar.campus.framework.api.file.FileUploadUtils;
import com.oddfar.campus.framework.api.file.MimeTypeUtils;
import com.oddfar.campus.framework.api.sysconfig.ConfigExpander;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import static com.oddfar.campus.common.utils.SecurityUtils.getLoginUser;

@RestController
@RequestMapping("/fjdcgl/wzxx")
public class WzxxController {

    @Autowired
    private WzxxService wzxxService;

    @Autowired
    private WzxxMapper wzxxMapper;

    @RequestMapping(value = "/list", name = "查询违章列表")
    @PreAuthorize("@ss.hasAnyRoles('common,admin,user')")
    public R list(Wzxx wzxx) {
        PageResult<Wzxx> page = wzxxService.page(wzxx);
        return R.ok().put(page);
    }

    @RequestMapping(value = "/cl/list", name = "查询违章列表")
    @PreAuthorize("@ss.hasAnyRoles('common,admin,user')")
    public R cllist(Wzxx wzxx) {
        PageResult<Wzxx> page = wzxxService.clpage(wzxx);
        return R.ok().put(page);
    }

    /**
     * 获取违章详细信息
     */
    @PreAuthorize("@ss.hasAnyRoles('common,admin,user')")
    @GetMapping(value = "/{mobile}", name = "获取违章详细信息")
    public R getInfo(@PathVariable("mobile") Long mobile) {
        return R.ok(wzxxMapper.selectById(mobile));
    }

    /**
     * 新增违章信息
     */
    @PreAuthorize("@ss.hasAnyRoles('admin')")
    @PostMapping(name = "新增违章信息")
    public R add(@RequestBody Wzxx wzxx) {
        return R.ok(wzxxService.insertWzxx(wzxx));
    }

    /**
     * 修改违章信息
     */
    @PreAuthorize("@ss.hasAnyRoles('admin')")
    @PutMapping(name = "修改违章信息")
    public R edit(@RequestBody Wzxx wzxx) {
        return R.ok(wzxxService.updateWzxx(wzxx));
    }

    /**
     * 删除违章信息
     */
    @PreAuthorize("@ss.hasAnyRoles('admin')")
    @DeleteMapping(value = "/{djxh}", name = "删除违章信息")
    public R remove(@PathVariable Long djxh) {
        return R.ok(wzxxMapper.deleteWzxx(djxh));
    }

    /*
    uploadImg
     */
    @PreAuthorize("@ss.hasAnyRoles('common,admin,user')")
    @PostMapping(value = "/uploadImg", name = "上传图片")
    public R uploadImg(@RequestParam("wzpic") MultipartFile wzpic) throws Exception {
        if (!wzpic.isEmpty()) {
            String pic = FileUploadUtils.upload(ConfigExpander.getAvatarPath(), wzpic, MimeTypeUtils.IMAGE_EXTENSION);
            if (wzxxService.updateUserAvatar(pic)) {
                R ajax = R.ok();
                ajax.put("imgUrl", pic);
                return ajax;
            }
        }
        return R.error("上传图片异常，请联系管理员");
    }
    
}
